/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdCurrency;

import com.SySammy.SdCurrency.Handlers.BankHandler;
import com.SySammy.SdCurrency.Handlers.LoanRun;
import com.SySammy.SdCurrency.Handlers.RepeatingTask;
import com.SySammy.SdCurrency.Prop.CurrencyProperties;
import com.SySammy.SdCurrency.commands.sdCBankCmds;
import com.SySammy.SdCurrency.commands.sdCMoneyCmds;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

public class sdC extends JavaPlugin {

    public static void main(String[] args) {
        // TODO Debug
    }
    private static final Logger log = Logger.getLogger("Minecraft");
    private sdPlayerListener PL = new sdPlayerListener();
    //Files & dirs
    private static final String currencyDIR = "plugins/RPGmod/currency/";
    private static final File accounts = new File(currencyDIR + "accounts.txt");
    private static final File loans = new File(currencyDIR + "loans.txt");
    public PermissionHandler Permissions;
    private boolean UsePermissions;

    @Override
    public void onDisable() {
        System.out.println("disabled, goodbye wold");
    }

    @Override
    public void onEnable() {
        PluginDescriptionFile pdfFile = this.getDescription();
        PluginManager pm = getServer().getPluginManager();
        new File(currencyDIR).mkdirs();
        if (!accounts.exists()) {
            try {
                accounts.createNewFile();
            } catch (IOException ex) {
                log.log(Level.INFO, ex.getMessage(), pdfFile.getName());
            }
        }
        if (!loans.exists()) {
            try {
                loans.createNewFile();
            } catch (IOException ex) {
                log.log(Level.INFO, ex.getMessage(), pdfFile.getName());
            }
        }
        pm.registerEvent(Event.Type.PLAYER_JOIN, this.PL, Priority.Normal, this);
        CurrencyProperties.loadCurrencyProps(false, 0);
        ContinueLoans();
        setupPermissions();
        getCommand("money").setExecutor(new sdCMoneyCmds(this));
        getCommand("bank").setExecutor(new sdCBankCmds(this));
        System.out.println(pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!");
    }

    private void setupPermissions() {
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");

        if (this.Permissions == null) {
            if (test != null) {
                UsePermissions = true;
                this.Permissions = ((Permissions) test).getHandler();
                System.out.println("Permissions is enable for sdBountyHunter.");
            } else {
                log.info("Permission system not detected, defaulting to OP");
                UsePermissions = false;
            }
        }
    }

    private void ContinueLoans() {
        BankHandler BH = new BankHandler();
        if (BH.GetLoans() != null) {
            for (int x = 0; x < BH.GetLoans().toArray().length; x++) {
                String imsg = BH.GetLoans().toArray()[x].toString();
                String[] msg = imsg.split("=");
                String[] msg2 = msg[1].split(";");
                int Schedule = this.getServer().getScheduler().scheduleSyncDelayedTask(this, new LoanRun(msg[0], this), (Integer.parseInt(msg2[4]) * 86400 * 20L));
                int ScheduleRep = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new RepeatingTask(msg[0], this), CurrencyProperties.ScheduleRepTime * 3600 * 21L, CurrencyProperties.ScheduleRepTime * 3600 * 20L);
                BH.SetSchedule(msg[0], Schedule);
                BH.SetScheduleRep(msg[0], ScheduleRep);
            }
        }
    }

    public boolean casUseMoneyInfo(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "money.info");
        }
        return p.isOp();
    }

    public boolean casUseMoneyCheck(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "money.check");
        }
        return p.isOp();
    }

    public boolean casUseMoneyPay(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "money.pay");
        }
        return p.isOp();
    }

    public boolean casUseMoneyHelp(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "money.help");
        }
        return p.isOp();
    }

    public boolean casUseBankInfo(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.info");
        }
        return p.isOp();
    }

    public boolean casUseBankLoan(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.loan");
        }
        return p.isOp();
    }

    public boolean casUseBankRepay(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.repay");
        }
        return p.isOp();
    }

    public boolean casUseBankHelp(Player p) {
        if (UsePermissions) {
            return this.Permissions.has(p, "bank.help");
        }
        return p.isOp();
    }
}
